package com.sqfw.project.wp.controller;


import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.wp.domain.WpHurryLog;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.service.IWpInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 工单表Controller
 *
 * @author lx
 * @date 2023-06-07
 */
@Api(tags = "审核人工单controller")
@RestController
@RequestMapping("/wp/reviewerWp")
public class ReviewerWpInfoController extends BaseController {

    @Autowired
    private IWpInfoService wpInfoService;



    /**
     * 审核人未处理列表（可回收/催单/已催单列表）
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('reviewerUntreatedList')")
    @GetMapping("/reviewerUntreatedList")
    public TableDataInfo reviewerUntreatedList(WpInfo wpInfo) throws ParseException {
        startPage();
        List<WpInfo> list = wpInfoService.selectReviewerUntreatedList(wpInfo);
        return getDataTable(list);

    }

    /**
     * 已催单列表
     * @param wpHurryLog
     * @return
     * @throws ParseException
     */
    @PreAuthorize("@ss.hasPermi('reviewerReminderList')")
    @GetMapping("/reviewerReminderList")
    public TableDataInfo reviewerReminderList(WpHurryLog wpHurryLog) {
        startPage();
        List<WpInfo> list = wpInfoService.reviewerReminderList(wpHurryLog);
        return getDataTable(list);

    }

    /**
     * 审核人已处理列表
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('reviewerTreatedList')")
    @GetMapping("/reviewerTreatedList")
    public TableDataInfo reviewerTreatedList(WpInfo wpInfo) throws ParseException {
        //获取当前登录人信息
        startPage();
        TableDataInfo tableDataInfo = wpInfoService.selectprocessedWpInfoList(wpInfo);
        return tableDataInfo;

    }

    /**
     * 审核人处理
     * @param wpInfo
     * @return
     */
    @Log(title = "审核人处理工单表", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('reviewerEdit')")
    @PostMapping("/reviewerEdit")
    public AjaxResult reviewerEdit(@RequestBody WpInfo wpInfo){

        return wpInfoService.updateWpInfo(wpInfo);
    }

    /**
     * 审核人催单
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('reminderEdit')")
    @PostMapping("/reminderEdit")
    public AjaxResult reminderEdit(@RequestBody WpInfo wpInfo){
        SysUser user = SecurityUtils.getLoginUser().getUser();

        wpInfo.setHurryName(user.getCode());

        WpInfo wpInfoCon = wpInfoService.selectWpInfoByScid(wpInfo.getScid());
        if ("承办单位处理".equals(wpInfoCon.getNextState())||"承办单位领导审核".equals(wpInfoCon.getNextState())){

            return toAjax(wpInfoService.reminderEdit(wpInfo));
        }else {
            return AjaxResult.error("该工单已不在承办单位，无需催单！");
        }

    }

    /**
     * 审核人收回工单
     * @param wpInfo
     * @return
     */
    @Log(title = "审核人收回工单表", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('recoverEdit')")
    @PostMapping("/recoverEdit")
    public AjaxResult recoverEdit(@RequestBody WpInfo wpInfo){

        wpInfo.setLastmessage("审核人收回工单");
        wpInfo.setState("审核人收回工单");
        wpInfo.setNextState("审核人处理");
        //收回工单状态
        wpInfo.setRecoverState("1");


        return wpInfoService.updateWpInfo(wpInfo);
    }

    /**
     * 审核人延期
     * @param wpInfo
     * @return
     */
    @Log(title = "审核人延期工单表", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('reviewerWp:extensionEdit')")
    @PostMapping("/extensionEdit")
    public AjaxResult extensionEdit(@RequestBody WpInfo wpInfo){

        return toAjax(wpInfoService.extensionEdit(wpInfo));

    }



}
